import Vue from 'vue'
import { login, getPermission } from "@/api/user"
import { USER_TOKEN,USER_TOKEN1,USER_INFO } from '@/common/util/constants.js'

const user = {
	state: {
		token: '',
		token1: '',
		userinfo: {},
		isLogin: false,
		update: {},
		permission:[]
	},

	mutations: {
		SET_UPDATE: (state, update) => {
			state.update = update;
		},
		SET_TOKEN: (state, token) => {
			state.token = token;
		},
		SET_TOKEN1: (state, token) => {
			state.token1 = token;
		},
		SET_USER_INFO: (state, userinfo) => {
			state.userinfo = userinfo;
		},
		SET_IS_LOGIN: (state, status) => {
			state.isLogin = status;
		},
		SET_PERMISSION: (state, permission) => {
			state.permission = permission;
		},
	},

	actions: {
		setPermission({ commit }){
			return new Promise((resolve, reject) => {
			  getPermission().then(res=>{
			  	resolve(res.data.data)
			  	commit('SET_PERMISSION',res.data.data);
				uni.setStorageSync('SET_PERMISSION',res.data.data);
			  }).catch(err=>{
			  	reject(err)
			  })
			})
		},
		setUpdate({ commit }, update){
			commit('SET_UPDATE', update);
		},
		isLoginAction({ commit }, status) {
			commit('SET_IS_LOGIN', status);
		},
		loginAction({ commit }, userinfo) {
			return new Promise((resolve, reject) => {
				login(userinfo).then(res => {
					if(res.data.code==200){
						let token = res.data.result.token;
						let userinfo = res.data.result.userInfo;
						commit('SET_TOKEN', token);
						uni.setStorageSync(USER_TOKEN,token);
						// commit('SET_TOKEN1', res.data.data.auth_sign);
						// uni.setStorageSync(USER_TOKEN1,res.data.data.auth_sign);
						commit('SET_USER_INFO', userinfo);
						uni.setStorageSync(USER_INFO,userinfo);
					}
					resolve(res);
				}).catch((err) => {
					reject(err);
				})
			})
			
		},
		userInfoAction({ commit }, userinfo) {
			commit('SET_USER_INFO', userinfo);
			uni.setStorageSync(USER_INFO,userinfo);
			//console.log('getStorageSync--->',uni.getStorageSync(USER_INFO))
		},
	}
}

export default user
